//
//  UserComponentVC.m
//  CoolTennisBall
//
//  Created by Coollang on 16/8/29.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "UserTwoColumnPickerVC.h"
#import "UserGroup.h"
#import "UserItem.h"
#import "NSString+Extension.h"


@interface UserTwoColumnPickerVC ()<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource>
/** textField */
@property (nonatomic, strong) FUITextField *textField;

@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic,strong)NSString *tempKey;
@property (nonatomic,strong)NSArray *tempValues;

@property (nonatomic, strong) NSArray *allKeys;
@property (nonatomic, copy) NSString *string2;

@end

@implementation UserTwoColumnPickerVC {
    BOOL _isChinese;
    NSArray *_monthArr;
    __block NSInteger index;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _isChinese = [NSBundle currentLanguageIsChinese];
    _monthArr = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    self.title = self.contentKey;
    self.view.backgroundColor = [UIColor colorWithHex:0xf3f3f3];
    
    self.string2 =  self.secondColValue;
    self.tempKey = self.firstColValue;
    
    if (!_isChinese) {
        self.tempKey = [self.tempKey trimingAllNumber];
        self.string2 = [self.secondColValue trimingAllNumber];
        NSInteger monthNum = [[self.secondColValue trimingAllNumber] integerValue];
        if (monthNum > 0) {
            NSArray *months = self.pickerDataSource[self.tempKey];
            self.string2 = months[monthNum - 1];
        }
    }
    
    self.allKeys = [self.pickerDataSource.allKeys sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:nil ascending:YES]]];
    [self setTextFieldUpUI];
    [self setPickerViewUpUI];
    
    
    [self configureNav];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self configerUPUi];
}

// 初始化界面
- (void)configerUPUi {
    
    NSLog(@"%@%@",self.string2,self.tempKey);
    
    [self.allKeys enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([self.tempKey isEqualToString:obj]) {
            [self.pickerView selectRow:idx inComponent:0 animated:NO];
            [self pickerView:self.pickerView didSelectRow:idx inComponent:0];
            index = idx;
            *stop = YES;
        }
    }];
    
    [self.pickerDataSource[self.tempKey] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if ([self.string2 isEqualToString:obj]) {
            [self.pickerView selectRow:idx inComponent:1 animated:NO];
            [self pickerView:self.pickerView didSelectRow:idx inComponent:1];
            *stop = YES;
        }
    }];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (index == self.allKeys.count - 1) {
        [self.pickerView selectRow:index inComponent:0 animated:YES];
        [self pickerView:self.pickerView didSelectRow:index inComponent:0];
    }
}

- (void)configureNav{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sport_icon_blackbackArrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [self.textField becomeFirstResponder];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    if (self.selectCallback && self.textField.text.length > 0) {
        NSArray *months = self.pickerDataSource[self.allKeys.firstObject];
    
        NSInteger month = [months indexOfObject:self.string2];
        
        NSString *callBackStr = nil;
        if (_isChinese) {
            callBackStr = self.textField.text;
        }else {
            callBackStr = [NSString stringWithFormat:@"%@-%@",[self.textField.text trimingAllNumber], _monthArr[month]];
        }
        self.selectCallback(callBackStr);
    }
    
}

- (void)setTextFieldUpUI {
    FUITextField *textField = [[FUITextField alloc] initWithFrame:CGRectMake(0, kAutoHei(24), kYFScreenWidth, kAutoHei(44))];
    
    self.textField = textField;
    self.textField.tintColor = [UIColor clearColor];
    textField.textColor = [UIColor colorWithHex:0x787878];
    textField.font = [UIFont systemFontOfSize:16];
    
    
    textField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.textField];
    textField.delegate = self;
    
    UILabel *leftLabel = [[UILabel alloc] init];
    leftLabel.text = self.contentKey;
    
    leftLabel.textColor = [UIColor colorWithHex:0x151515];
    leftLabel.font = [UIFont systemFontOfSize:18];
    
    [leftLabel sizeToFit];
    CGFloat labelW = leftLabel.bounds.size.width;
    
    leftLabel.frame = CGRectMake(0, 0,(labelW + kAutoWid(40)) , kAutoHei(44));
    leftLabel.textAlignment = NSTextAlignmentCenter;
    
    self.textField.leftView = leftLabel;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    textField.edgeInsets = UIEdgeInsetsMake(0, kAutoWid(10), 0, 0);
    
    self.textField.text = [NSString stringWithFormat:@"%@%@", [NSString checkIfNullWithString:self.firstColValue],[NSString checkIfNullWithString:self.secondColValue]];
    //    添加分隔线
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0,kAutoHei(24) - 0.5, kYFScreenWidth, 0.5)];
    topLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:topLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(textField.frame), kYFScreenWidth, 0.5)];
    bottomLine.backgroundColor = kLineBackgroundColor;
    [self.view addSubview:bottomLine];
    
}

- (void)setPickerViewUpUI {
    
    UIPickerView *pickView = [[UIPickerView alloc] init];
    self.pickerView = pickView;
    pickView.dataSource = self;
    pickView.delegate = self;
    
    self.textField.inputView = pickView;
    self.pickerView.backgroundColor = [UIColor whiteColor];
}
#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.allKeys.count;
    }else {
        NSInteger index = [pickerView selectedRowInComponent:0];
        self.tempKey = self.allKeys[index];
        self.tempValues = self.pickerDataSource[self.tempKey];
        return self.tempValues.count;
    }
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        return self.allKeys[row];
    }else{
        return self.tempValues[row];
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        self.tempKey = self.allKeys[row];
    }else {
        self.string2 = self.tempValues[row];
    }

    if (self.string2.length <= 0) {
        NSArray *months = self.pickerDataSource[self.tempKey];
        self.string2 = months[0];
    }
    if (_isChinese) {
        self.textField.text = [NSString stringWithFormat:@"%@%@",self.tempKey, self.string2];
    }else {
        self.textField.text = [NSString stringWithFormat:@"%@ %@",[self.string2 substringToIndex:3], self.tempKey];
    }
}
@end
